package cn.jietuo.juc.c_013_reftypeAndThreadLocal;

import java.lang.ref.SoftReference;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2020/5/11
 * @description: 软引用
 */
public class T02_SoftReference {

    public static void main(String[] args) {
        System.out.println("heheh");
        // 10M的大小
        SoftReference<byte[]> m = new SoftReference<>(new byte[1024 * 1024 * 10]);
        System.out.println(m.get());
        System.gc();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(m.get());
        // 在分配一个数组，heap装不下，这时候，系统就会垃圾回收，先回收一次,如果不够,会把软引用干掉
        byte[] bytes = new byte[1024 * 1024 * 18];
        System.out.println(m.get());
    }
}
